package com.dg.river.core.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dg.river.R;
import com.dg.river.core.util.LogUtils;


/**
 * 类描述：自定义加载框dialog
 *
 * @author
 * @date 2016-8-30
 */
public class ProgressDialog extends Dialog {

    public ImageView imageView;
    public AnimationDrawable spinner;
    public CharSequence message;

    public ProgressDialog(Context context) {
        super(context);
    }

    public ProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public ProgressDialog dialog;
    public ImageView imageViewBg;
    public LinearLayout ll_white_bg;
    public Context mContext;

    /**
     * 当窗口焦点改变时调用
     */
    public void onWindowFocusChanged(boolean hasFocus) {
        if (spinner == null) {
            spinner = (AnimationDrawable) getContext().getResources().getDrawable(R.drawable.progress_spinner);
            if (imageView == null) {
                imageView = findViewById(R.id.spinnerImageView);
            }
            imageView.setImageDrawable(spinner);
        }
        // 开始动画
        spinner.start();

        LogUtils.e("log===============onWindowFocusChanged======================");
    }

    /**
     * 给Dialog设置提示信息
     *
     * @param message
     */
    public void setMessage(CharSequence message) {
        if (message != null && message.length() > 0) {
            this.message = message;
        }
    }

    /**
     * 弹出自定义ProgressDialog
     *
     * @param context        上下文
     * @param msg            提示
     * @param cancelable     是否按返回键取消
     * @param cancelListener 按下返回键监听
     * @return
     */
    public ProgressDialog show(final Context context, String msg, boolean cancelable, OnCancelListener cancelListener) {
        mContext = context;
        message = msg;

        dialog = new ProgressDialog(context, R.style.customProgress);

        Activity activity = (Activity) context;
        dialog.setTitle("");
        dialog.setContentView(R.layout.progress_dialog);
        if (message != null) {
            TextView txt = dialog.findViewById(R.id.message);
            txt.setText(message);
        }
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setOnCancelListener(cancelListener);

        if (dialog.getWindow() != null) {
            dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

            int height;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                DisplayMetrics dm = new DisplayMetrics();
                activity.getWindowManager().getDefaultDisplay().getRealMetrics(dm);
                height = dm.heightPixels;
            } else {
                DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
                height = metrics.heightPixels;
            }
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = height;
            dialog.getWindow().setAttributes(lp);
        }

        imageViewBg = dialog.findViewById(R.id.iv_bg);
        ll_white_bg = dialog.findViewById(R.id.ll_white_bg);

//        spinner = (AnimationDrawable) mContext.getResources().getDrawable(R.drawable.progress_spinner);
//        if (imageView == null) {
//            imageView = dialog.findViewById(R.id.spinnerImageView);
//        }
//        imageView.setImageDrawable(spinner);
//        // 开始动画
//        spinner.start();

        imageViewBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (spinner != null) {
                    spinner.stop();
                    spinner = null;
                    imageView = null;
                }
                dialog.dismiss();
                dialog.cancel();
            }
        });

        try {
            dialog.show();
            hideToShowImage(imageViewBg, 500);
            hideToShowCenter(ll_white_bg, 500);
        } catch (Exception e) {

        }
        return dialog;
    }

    /**
     * 动画
     *
     * @param view
     */
    public void hideToShowImage(View view, int time) {
        Animation slide = new AlphaAnimation(0f, 0.4f);
        slide.setDuration(time);
        slide.setFillAfter(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 动画
     *
     * @param view
     */
    public void hideToShowCenter(View view, int time) {
        Animation slide = new AlphaAnimation(1.0f, 1.0f);
        slide.setDuration(time);
        slide.setFillAfter(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    /**
     * 动画
     *
     * @param view
     */
    public void showToHideCenter(View view, int time) {
        Animation slide = new AlphaAnimation(1.0f, 0f);
        slide.setDuration(time);
        slide.setFillAfter(true);
        slide.setInterpolator(new AccelerateInterpolator());
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (spinner != null) {
                    spinner.stop();
                    spinner = null;
                    imageView = null;
                }
                dialog.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    /**
     * 动画
     *
     * @param view
     */
    public void showToHideImage(View view, int time) {
        if (view != null) {
            Animation slide = new AlphaAnimation(0.4f, 0.0f);
            slide.setDuration(time);
            slide.setFillAfter(true);
            view.startAnimation(slide);

            slide.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    showToHideCenter(ll_white_bg, 700);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        } else {
            if (spinner != null) {
                spinner.stop();
                spinner = null;
                if (imageView != null) {
                    imageView = null;
                }
            }
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    }

    /**
     * dismiss
     */
    public void dialogDismiss() {
        showToHideImage(imageViewBg, 400);
    }
}
